package cn.seaboot.admin.quartz.service;

import cn.seaboot.admin.quartz.bean.ScheduleAction;
import cn.seaboot.admin.quartz.bean.ScheduleJob;
import cn.seaboot.admin.quartz.dao.ScheduleActionDao;
import cn.seaboot.admin.quartz.job.ScheduleRequestJob;
import cn.seaboot.commons.core.Asserts;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Service
 * 定时请求任务 [t_schedule_action]
 *
 * @author Mr.css on 2018-6-20
 */
@Service
public class ScheduleActionService {


    @Resource
    private ScheduleActionDao scheduleActionDao;

    @Resource
    private ScheduleJobService scheduleJobService;

    /**
     * 项目启动，恢复所有定时任务
     *
     * @throws SchedulerException 异常
     */
    @Transactional(rollbackFor = Exception.class)
    public void startScheduleActionJob() throws SchedulerException {
        List<ScheduleAction> actionList = queryList(Collections.emptyMap());

        for (ScheduleAction action : actionList) {
            // 如果数据不一致，说明有人动过数据库
            ScheduleJob pojo = scheduleJobService.queryById(action.getJobId());
            Asserts.notNull(pojo, "schedule-job is not found!");

            scheduleJobService.start(
                    pojo.getJobGroup(), pojo.getJobName()
                    , pojo.getTriggerGroup(), pojo.getTriggerName()
                    , ScheduleRequestJob.class, pojo.getCron());
        }
    }

    /**
     * 增
     *
     * @param pojo pojo
     * @return affected rows
     */
    @Transactional(rollbackFor = Exception.class)
    public int insert(ScheduleJob pojo, ScheduleAction action) throws SchedulerException {
        scheduleJobService.start(
                pojo.getJobGroup(), pojo.getJobName()
                , pojo.getTriggerGroup(), pojo.getTriggerName()
                , ScheduleRequestJob.class, pojo.getCron());

        action.setJobId(pojo.getId());

        action.setJobName(pojo.getJobName());
        action.setJobGroup(pojo.getJobGroup());
        action.setTriggerName(pojo.getTriggerName());
        action.setTriggerGroup(pojo.getTriggerGroup());

        return scheduleActionDao.insert(action);
    }

    /**
     * 删
     *
     * @param id id
     * @return affected rows
     */
    public int deleteById(Long id) {
        return scheduleActionDao.deleteById(id);
    }

    /**
     * 改
     *
     * @param pojo pojo
     * @return affected rows
     */
    public int updateById(ScheduleAction pojo) {
        return scheduleActionDao.updateSampleById(pojo);
    }

    /**
     * 查
     *
     * @param id id
     * @return pojo
     */
    public ScheduleAction queryById(Long id) {
        return scheduleActionDao.selectById(id);
    }

    /**
     * 查列表
     *
     * @param params 参数
     * @return list
     */
    public List<ScheduleAction> queryList(Map<String, Object> params) {
        return scheduleActionDao.selectList(params);
    }
}
